package com.mindbook.vip.gawxchatinfo.config;


import com.mindbook.vip.gawxchatinfo.config.shiro.CustomModularRealmAuthenticator;
import com.mindbook.vip.gawxchatinfo.config.shiro.CustomModularRealmAuthorizer;
import com.mindbook.vip.gawxchatinfo.config.shiro.UserBackRealm;
import com.mindbook.vip.gawxchatinfo.config.shiro.UserFrontRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * @Author wxb
 * @Datatime 2022/3/4 15:13
 * @Descript Shiro配置
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean ShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorizedUrl");
        shiroFilterFactoryBean.setSuccessUrl("/success");
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //添加过滤器链接
        Map<String,String> filterChainMap = new LinkedHashMap<>();
        //filterChainMap 的几种校验规则
        filterChainMap.put("/test/say","anon");
        filterChainMap.put("/test/say/data","anon");

        //微信接口
        filterChainMap.put("/wx/**","anon");

        //后端用户管理
        filterChainMap.put("/back/user/login","anon");//后端用户登录

        //文件
        filterChainMap.put("/file/","anon");//文件上传

        //前端用户管理
//        filterChainMap.put("/front/login","anon");//前端用户登录
        filterChainMap.put("/front/user/register","anon");//前端用户注册
        filterChainMap.put("/front/user/phoneNumber","anon");//查询当前微信用户的手机号
        
        //首页数据列表
        filterChainMap.put("/firstPage/list/details/list","anon");//首页数据列表
        //常问问题
        filterChainMap.put("/oftenAskQuestion/page","anon");//分页,questionStem模糊查询
        filterChainMap.put("/oftenAskQuestion/**","anon");//查询数据详情
        //秘芳
        filterChainMap.put("/nostrumType/treeList","anon");
        filterChainMap.put("/nostrum/function/list","anon");
        filterChainMap.put("/nostrum/function/link/**/**","anon");


        //swagger2
        filterChainMap.put("/swagger-ui.html", "anon");
        filterChainMap.put("/swagger/**","anon");
        filterChainMap.put("/swagger-resources/**", "anon");
        filterChainMap.put("/v2/**", "anon");
        filterChainMap.put("/webjars/**", "anon");
        filterChainMap.put("/configuration/**", "anon");
        filterChainMap.put("/doc.html","anon");



        //拦截剩下的所有的接口
        filterChainMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
        return shiroFilterFactoryBean;
    }



    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(){
        DefaultWebSecurityManager securityManage = new DefaultWebSecurityManager();
        securityManage.setAuthenticator(getCustomModularRealmAuthenticator());
        securityManage.setAuthorizer(getCustomModularRealmAuthorizer());

        //自定义缓存实现，使用redis
//        securityManage.setCacheManager(redisCacheManager());
        //自定义session管理，使用redis
        securityManage.setSessionManager(getSessionManager());

        //设置多个Realm 进行用户登录校验
        Collection<Realm> realms = new ArrayList<>();
        realms.add(getUserBackRealm());
        realms.add(getUserFrontRealm());

        securityManage.setRealms(realms);
        return securityManage;
    }

    /**
     * cacheManager 缓存 redis 实现
     *
     * 使用shiro-redis 插件
     * @return
     */
    @Bean
    public RedisCacheManager redisCacheManager(){
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(getRedisManager());
        return redisCacheManager;
    }

    /**
     * session Manager
     * 使用的是Shiro-redis 插件
     *
     * @return
     */
    @Bean
    public SessionManager getSessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionDAO(redisSessionDao());
        //设置用户登录保存时间
        sessionManager.setGlobalSessionTimeout(7*24*60*60*1000L);
        return sessionManager;
    }

    @Bean
    public RedisSessionDAO redisSessionDao(){
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(getRedisManager());
        return redisSessionDAO;
    }

    /**
     * 配置shiro-redis
     * @return
     */
    @Bean
    public RedisManager getRedisManager(){
        RedisManager redisManager = new RedisManager();
        redisManager.setDatabase(0);
        redisManager.setHost("localhost");
        redisManager.setPort(6379);
//        redisManager.setPassword("000000");
        redisManager.setExpire(60*60*24*7);//设置过期时间为7天
        redisManager.setTimeout(0);
        return redisManager;
    }

    @Bean
    public CustomModularRealmAuthenticator getCustomModularRealmAuthenticator(){
        CustomModularRealmAuthenticator customModularRealmAuthenticator = new CustomModularRealmAuthenticator();
        return customModularRealmAuthenticator;
    }

    @Bean
    public CustomModularRealmAuthorizer getCustomModularRealmAuthorizer(){
        CustomModularRealmAuthorizer authorizer = new CustomModularRealmAuthorizer();
        return authorizer;
    }


    @Bean
    public UserBackRealm getUserBackRealm(){
        UserBackRealm userBackRealm = new UserBackRealm();
        return userBackRealm;
    }

    @Bean
    public UserFrontRealm getUserFrontRealm(){
        UserFrontRealm userFrontRealm = new UserFrontRealm();
        return userFrontRealm;
    }

}
